﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Search_CommunityMonitorSrhForm
 * 类型：       控制层
 * 功能说明：   监控
 * 作者：       郭晓庆
 * 创建日期：   2008/02/01
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Search.Dao;
using Shpm.App.Search.Bean;
using Shpm.App.Input.Bean;
using Shpm.System;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Commons.Utils;
using System.Data.OleDb;
using Shpm.App.Third.Dao;
using Shpm.App.Third.Bean;
using Shpm.App.Master.Dao;
using Shpm.App.Input.Dao;
using Shpm.App.Master.Bean;

public partial class App_Search_CommunityMonitorSrhForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CommunityMonitorDao dao = new CommunityMonitorDao();
        IList dataList = dao.GetMapOfCctvList();
        cctvRepeater.DataSource = dataList;
        cctvRepeater.DataBind();

        if (!IsPostBack)
        {
            /*
            this.GridView1.DataSource = FindAlarm();
            this.GridView1.DataBind();
            this.GridView1.PageIndex = 0;
            */
            initAll();
        }
    }
    private IList FindAlarm()
    {
        IList list;

        try
        {
            AlarmDao dao = new AlarmDao();
            list = dao.FindUserAlarmHisRec();
        }
        catch
        {
            list = new ArrayList();

            string msg = Convert.ToString(HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "App_Search_Community_Monitor_Srh_AlarmDB_Error"));
            msg = string.Format("alert(\"{0}\")", msg);
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "javascript", msg, true);
        }

        return list;
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        string sendFlg = this.hidSendFlg.Value;
        if ("TRUE".Equals(sendFlg))
        {
            CommonDictionaryBean paramBean = new CommonDictionaryBean();
            paramBean.Catalog = "UserAlarmActive.UserID";
            CommonDictionaryDao dao = new CommonDictionaryDao();
            IList dataList = dao.FindByCatalog(paramBean);
            if (dataList.Count > 0 && CheckMemberNo(dataList))
            {
                SendMessage("报警事件--", (dataList[0] as CommonDictionaryBean).DictNo);
                ((Design_MasterPage)this.Master).InfoMessage = "发送成功！";
                this.hidSendFlg.Value = "FALSE";
            }
            else 
            {
                ((Design_MasterPage)this.Master).InfoMessage = "发送对象不存在，发送失败！";
            }
           
        }

    }

    private bool CheckMemberNo(IList dataList)
    {
        EmployeeDao dao = new EmployeeDao();
        EmployeeBean bean = new EmployeeBean();
        bean.EmployeeId = (dataList[0] as CommonDictionaryBean).DictNo;
        if (dao.GetEmployee(bean) == null)
            return false;
        return true;
    }
    //send message
    private void SendMessage(string subject, string toEmployeeId)
    {
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;



        MessageBean beanMsg = new MessageBean();
        beanMsg.Subject = subject + DateTime.Now.ToString();
        beanMsg.Message = this.hidUserID.Value + "(" + this.hidBeginTime.Value + ")：" + this.hidComments.Value;
        beanMsg.MessageType = 1;
        beanMsg.SendDate = DateTime.Now;
        beanMsg.ReplyFlag = false;
        beanMsg.ReaderHistoryFlag = false;
        beanMsg.ImportantFlag = false;
        beanMsg.EmployeeId = emplyId;
        beanMsg.UserId = loginId;
        CommonSendMessageUtils sendMsg = new CommonSendMessageUtils(beanMsg, toEmployeeId);
        sendMsg.SendMessage();
    }

    /*
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.GridView1.DataSource = FindAlarm();
        this.GridView1.DataBind();
    }
    */
    /*
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            UserAlarmActiveBean bean = (UserAlarmActiveBean)e.Row.DataItem;
            e.Row.Cells[0].Text=bean.UserID.Replace("#","号楼")+"室";
        }
    }
    */

    private void initAll()
    {
        MonitorDetailDao Dao = new MonitorDetailDao();
        Shpm.App.Search.Bean.MonitorDetailBean paramBean = new Shpm.App.Search.Bean.MonitorDetailBean();
        IList list = null;
        list = Dao.ListMonitorGroup(paramBean);

        DrpGroup.DataSource = list;
        DrpGroup.DataBind();
    }
}
